/**
 * Copyright 2015 Software innovation and R & D center. All rights reserved.
 * File Name: SysLoginService.java
 * Encoding UTF-8
 * Version: 0.0.1
 * History:	2015年1月22日
 */
package com.sirdc.modules.sys.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.core.exceptions.UserExistException;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.dao.SysLoginDao;
import com.sirdc.modules.sys.databean.SysUserDataBean;
import com.sirdc.modules.sys.entity.SysLogin;
import com.sirdc.modules.sys.filter.SysLoginFilter;
import com.sirdc.modules.sys.filter.SysUserFilter;
import com.sirdc.modules.sys.util.PasswordHelper;
import com.sirdc.modules.sys.util.RedisUtils;
import com.sirdc.modules.sys.util.SysGlobals;
import com.sirdc.modules.sys.util.SysGlobals.AccountStates;

/**
 * 
 * @author: huiyang.yu
 * @version Revision: 0.0.1
 * @Date: 2015年1月22日
 */
@Service
public class SysLoginService extends StringPKService<SysLogin> {
	
	@Autowired
	private SysLoginDao sysLoginDao;
//	@Autowired
//	private SysUserPermissionDao sysUserPermissionDao;
	@Autowired
    private PasswordHelper passwordHelper;
	
	@Override
	protected BaseDao<SysLogin, String> getDao() {
		return sysLoginDao;
	}

	public List<SysLogin> query(SysLoginFilter filter) {
		return sysLoginDao.query(filter);
	}
	
	@Override
	protected void beforeSave(SysLogin user) {
		user.setState(RedisUtils.getDictVal(SysGlobals.ACCOUNTSTATE, AccountStates.NOMAL, "name"));//初始化帐号状态
		//user.setPassword(RedisUtils.getDictVal(SysGlobals.INITPASSWORD, SysGlobals.BIGCLASS, "attr1"));//初始化密码
		passwordHelper.encryptPassword(user);//密码加密
		
		//判断用户是否已经存在
		if (isExistForUsername(user.getUsername())) {
			throw new UserExistException(user.getUsername());
		}
	}
	
	/**
	 *  根学号判断用户是否存在，存在返回ture
	 * @author: huiyang.yu
	 * @param sno
	 * @return
	 */
	public boolean isExistForSno(String sno) {
		SysLogin sysLogin = sysLoginDao.getLoginBySno(sno);
		if(sysLogin == null) {
			return false;
		}
		return true;
	}

	/**
	 * 根据用户的口令信息获取用户信息
	 * @author: weihuang.peng
	 * @param token
	 * @return
	 */
	public SysLogin getUserByLoginName(String token) {
		return sysLoginDao.getUserByLoginName(token);
	}
	
	/**
	 * 根据电话号码判断用户是否存在
	 * @author: weihuang.peng
	 * @param mobile
	 * @return
	 */
	public boolean isExistForMobile(String mobile) {
		SysLogin sysLogin = sysLoginDao.getLoginByMobile(mobile);
		if(sysLogin == null) {
			return false;
		}
		return true;
	}
	
	/**
	 * 根据邮箱判断用户是否存在
	 * @author: weihuang.peng
	 * @param mail
	 * @return
	 */
	public boolean isExistForEmail(String mail) {
		SysLogin sysLogin = sysLoginDao.getLoginByEmail(mail);
		if(sysLogin == null) {
			return false;
		}
		return true;
	}

	/**
	 * 更新其他信息
	 */
	public void updateLogin(SysUserDataBean databean) {
		//保存登陆信息
		SysLogin login = getById(databean.getUserId());
		if (databean.getDeptId() != null) {
			login.setDeptId(databean.getDeptId());
		}
		if (databean.getRoleId() != null) {
			login.setRoleId(databean.getRoleId());
		}

		if ((databean.getPassword() != null) && (!login.getPassword().equals(databean.getPassword()))) {
			login.setPassword(databean.getPassword());
			passwordHelper.encryptPassword(login);
		}
		merge(login);
	}
	
	/**
	 * 返回加密后的新密码
	 * @param password
	 * @param salt
	 * @return
	 */
	public String getEncryPass(String password, String salt) {
		return passwordHelper.encryptPassResult(password, salt);
	}

	/**
	 * 更新密码
	 * @author: weihuang.peng
	 * @param databean
	 */
	public void updatePassword(SysUserDataBean databean) {
		SysLogin login = getById(databean.getUserId());
		login.setPassword(databean.getPassword());
		if (!login.getPassword().equals(databean.getOldPassword())) {
			passwordHelper.encryptPassword(login);
		}
		sysLoginDao.update(login);
	}

	/**
	 * @author: weihuang.peng
	 * @param filter
	 */
	public List<String> queryLoginByRoleDept(SysUserFilter filter) {
		return sysLoginDao.queryLoginByRoleDept(filter);
	}

	/**
	 * @author: weihuang.peng
	 * @param userId
	 * @param roleId
	 * @param deptId
	 * @return
	 */
	public boolean isExistUserPermission(String userId, String roleId, String deptId) {
		return sysLoginDao.isExistUserPermission(userId, roleId, deptId);
	}

	public boolean isExistForUsername(String username) {
		SysLogin sysLogin = sysLoginDao.getLoginByUsername(username);
		if(sysLogin == null) {
			return false;
		}
		return true;
	}
}
